package thread;

import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-08-01
 * Time: 16:53
 */
public class Demo28 {
    public static void main(String[] args) throws InterruptedException {
        //计数器初始值为4
        Semaphore semaphore = new Semaphore(4);
        semaphore.acquire();//计数器-1
        System.out.println("执行P操作");
        semaphore.acquire();//计数器-1
        System.out.println("执行P操作");
        semaphore.acquire();//计数器-1
        System.out.println("执行P操作");
        semaphore.acquire();//计数器-1
        System.out.println("执行P操作");
        semaphore.acquire();//计数器的值为0, 阻塞等待
        System.out.println("执行P操作");
    }
}
